<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
class Push extends Model{	
    public function url($sid=1,$info='',$type='add',$data=1){
		$config=F('_send/config');
		if(empty($config['send']['send_baidu_token']) && empty($config['send']['send_xiongzhang_appid']) && empty($config['send']['send_xiongzhang_token'])){	
		   return false;	
		}
		if(empty($config['send']['send_data']) && $data==1){
			return false;
		}
		if(empty($config['send']['send_cai']) && $data==2){
			return false;
		}		
        if(!empty($sid) && !empty($info)){
            if($sid==1){
				if(empty($info['vod_id'])){
			        $info=db('vod')->field('vod_id,vod_cid,vod_letters')->find($info);
				}
			    if($info['vod_id']){			
			          $url=zanpian_read_url('home/vod/read',$info,'',true,true);
	            }
			}
            if($sid==4){
				if(empty($info['story_id'])){
			        $info=db('story')->join('vod vod','vod.vod_id =story_vid','LEFT')->field('vod_id,vod_cid,vod_letters,story_id,story_vid,story_cid')->find($info);
				}
			    if($info['story_id']){			
			          $url=zanpian_read_url('home/story/read',$info,'',true,true);
	            }
			}
            if($sid==12){
				if(empty($info['part_id'])){
			        $info=db('part')->join('story story','story.story_vid =part_vid','LEFT')->join('vod vod','vod.vod_id =part_vid','LEFT')->field('vod_id,vod_cid,vod_letters,story_id,story_vid,story_cid,part_id,part_vid,part_oid')->find($info);
				}
			    if($info['part_id']){			
			          $url=zanpian_read_url('home/part/read',$info,'',true,true);
	            }
			}
            if($sid==6){
				if(empty($info['actor_id'])){
			        $info=db('actor')->join('vod vod','vod.vod_id =actor_vid','LEFT')->field('vod_id,vod_cid,vod_letters,actor_id,actor_vid,actor_cid')->find($info);
				}
			    if($info['actor_id']){			
			          $url=zanpian_read_url('home/actor/read',$info,'',true,true);
	            }
			}
            if($sid==5){
				if(empty($info['role_id'])){
			        $info=db('role')->join('actor actor','actor.actor_vid =role_vid','LEFT')->join('vod vod','vod.vod_id =role_vid','LEFT')->field('vod_id,vod_cid,vod_letters,actor_id,actor_vid,actor_cid,role_id,role_vid,role_cid')->find($info);
				}
			    if($info['role_id']){			
			          $url=zanpian_read_url('home/role/read',$info,'',true,true);
	            }
			}
            if($sid==3){
				if(empty($info['star_id'])){
			        $info=db('star')->field('star_id,star_cid,star_letters,star_jumpurl')->find($info);
				}
			    if($info['star_id']){			
			          $url=zanpian_read_url('home/star/read',$info,'',true,true);
	            }
			}
            if($sid==2){
				if(empty($info['news_id'])){
			        $info=db('news')->field('news_id,news_cid')->find($info);
				}
			    if($info['news_id']){			
			          $url=zanpian_read_url('home/news/read',$info,'',true,true);
	            }
			}
            if($sid==10){
				if(empty($info['special_id'])){
			        $info=db('news')->field('special_id,special_cid,special_letters')->find($info);
				}
			    if($info['special_id']){			
			          $url=zanpian_read_url('home/special/read',$info,'',true,true);
	            }
			}
            if($sid==7){
				if(empty($info['tv_id'])){
			        $info=db('tv')->field('tv_id,tv_cid,tv_letters')->find($info);
				}
			    if($info['tv_id']){			
			          $url=zanpian_read_url('home/tv/read',$info,'',true,true);
	            }
			}
            if($sid==15){
				if(empty($info['music_vid'])){
			        $info=db('vod')->field('vod_id,vod_cid,vod_letters')->find($info);
				}
			    if($info['vod_id']){			
			          $url=zanpian_read_url('home/vod/music',$info,'',true,true);
	            }
			}
            if($sid==14){
				if(empty($info['lines_vid'])){
			        $info=db('vod')->field('vod_id,vod_cid,vod_letters')->find($info);
				}
			    if($info['vod_id']){			
			          $url=zanpian_read_url('home/vod/lines',$info,'',true,true);
	            }
			}			
			return $this->send($url,$type,$data);			
		}		
    }
    public function urls($urls,$type='add',$data=''){
		$config=F('_send/config');
		if(empty($config['send']['send_baidu_token']) && empty($config['send']['send_xiongzhang_appid']) && empty($config['send']['send_xiongzhang_token'])){	
		   return false;	
		}		
        if(!empty($urls)){
			return $this->send($urls,$type);
	    }
    }	
    public function send($url,$type='add',$data=''){
		$config=F('_send/config');
		if(!empty($config['send']['send_domain'])){
			$domain=$config['send']['send_domain'];
		}else{
			$domain=Request()->host();
		}
        if(is_array($url)){
             $url = $url;
        }else{
             $url = array($url);	
        }
		if(!empty($config['send']['send_domain'])){
			foreach($url as $key=>$value){
				$urls[$key]=str_replace(Request()->domain(),$domain,$value);
			}
		}else{
			$urls=$url;
		}
		if(empty($config['send']['send_baidu_token']) && empty($config['send']['send_xiongzhang_appid']) && empty($config['send']['send_xiongzhang_token'])){	
		   return false;	
		}
		if(empty($config['send']['send_data']) && $data==1){
			return false;
		}
		if(empty($config['send']['send_cai']) && $data==2){
			return false;
		}
		if($type=='add'){
		    $types="添加";
		    $bidutui="http://data.zz.baidu.com/urls?site=".$domain."&token=".$config['send']['send_baidu_token'];	
		    $xiongzhang="http://data.zz.baidu.com/urls?appid=".$config['send']['send_xiongzhang_appid']."&token=".$config['send']['send_xiongzhang_token']."&type=realtime";	
		}else{
		    $types="更新";	
		    $bidutui="http://data.zz.baidu.com/urls?site=".$domain."&token=".$config['send']['send_baidu_token'];	
		    $xiongzhang="http://data.zz.baidu.com/urls?appid=".$config['send']['send_xiongzhang_appid']."&token=".$config['send']['send_xiongzhang_token']."&type=batch";		
		}
		if($config['send']['send_type']==3 || $config['send']['send_type']==1){	
            $ch = curl_init();
            $options =  array(
            CURLOPT_URL => $bidutui,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
	        CURLOPT_CONNECTTIMEOUT => 8,
	        CURLOPT_TIMEOUT => 6,
            CURLOPT_POSTFIELDS => implode("\n", $urls),
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
           );
           curl_setopt_array($ch, $options);
           $result = curl_exec($ch);
           $success=json_decode($result,true);
           curl_close($ch);
           if($success['success']){
			    $msg.="百度推送".$types."<font color=red>".$success['success']."</font>条成功&nbsp;&nbsp;剩余<font color=red>".$success['remain']."</font>条&nbsp;&nbsp;";     
            }else{
                $msg.= "百度".$types."失败".$success['message']."&nbsp;&nbsp;";    
            }
		}
		if($config['send']['send_type']==3 || $config['send']['send_type']==2){
            $chs = curl_init();
            $optionss =  array(
            CURLOPT_URL => $xiongzhang,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
	        CURLOPT_CONNECTTIMEOUT => 8,
	        CURLOPT_TIMEOUT => 6,
            CURLOPT_POSTFIELDS => implode("\n", $urls),
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
            );
            curl_setopt_array($chs, $optionss);
            $results = curl_exec($chs);
            $successs=json_decode($results,true);
            curl_close($chs);
		    if($type=='add'){
                if($successs['success_realtime']){
			        $msg.="熊掌推送新增<font color=red>".$successs['success_realtime']."</font>条成功&nbsp;&nbsp;剩余<font color=red>".$successs['remain_realtime']."</font>条&nbsp;&nbsp;";   
			    }else{
			        $msg.= "熊掌推送失败".$successs['message']."&nbsp;&nbsp;";
			    }
		    }else{
                if($successs['success_batch']){
				    $msg.="熊掌推送历史内容<font color=red>".$successs['success_batch']."</font>条成功&nbsp;&nbsp;剩余<font color=red>".$successs['remain_batch']."</font>条&nbsp;&nbsp;";   
			    }else{
				    $msg.= "熊掌推送历史内容失败".$successs['message']."&nbsp;&nbsp;";
			    }			
		    }
        }
        return	$msg;	

    }		
}
?>